<?php
Zend_Loader::loadClass('Application_Form');
Zend_Loader::loadClass('Application_Form_Element_ConstructionControls');

class Application_Form_Costruzioni extends Application_Form
{
	protected $_formDecorators = array(
		'FormElements',
		'Form',
		array('HtmlTag', array('tag' => 'div', 'id' => 'form-wrap')),
	);

	protected $_displaygroupDecorators = array(
		'FormElements',
		'Fieldset',
	);

	protected $_elementDecorators = array(
		'viewHelper',
		array(array('htmltag1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-span')),
		array('Label', array('tag' => 'div', 'tagClass' => 'form-span'))
	);

	public function __construct() {
		parent::__construct();
		$this->addJavascript("/validator/jquery.validationEngine.js");
		$this->addJavascript("/validator/languages/jquery.validationEngine-en.js");
		$this->addJavascript("/dataTables/jquery.dataTables.min.js");
		
		$this->addStylesheet("/validator/validationEngine.jquery.css");
		$this->addStylesheet("/dataTables/jquery.dataTables.css");
		$this->addStylesheet("/dataTables/demo_table_jui.css");
		$this->addStylesheet("/dataTables/fix.css");
	}

	public function run() {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		if (!$request->isXmlHttpRequest()) {
			// Contesto non-AJAX
			$this->_initForm();
			$this->_view->browserForm = $this->_form->getSubForm("browser")->render($this->_view);
			$this->_view->detailForm = $this->_form->getSubForm("detail")->render($this->_view);
			return;
		} else {
			// Contesto AJAX
			$layout = Zend_Layout::getMvcInstance();
			$layout->disableLayout();
		
			$ret = "";
			if ($request->isPost()) {
				// POST data
				foreach ($request->getParams() as $paramName => $paramValue) {
					$urlParams[$paramName] = urlencode($paramValue);
				}
				$this->_initForm();
				if ($this->_form->getSubForm("detail")->isValid($request->getParams())) {
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					$redirector->setGotoRoute($urlParams, 'ajax');
					$redirector->redirectAndExit();
				} else {
					$messages = $this->_form->getSubForm("detail")->getMessages();
					if (array_key_exists('construction_name', $messages)) {
						array_unshift($messages['construction_name'], "Corregere gli errori segnalati");
					} else {
						$messages['construction_name'] = array ("generic" => "Corregere gli errori segnalati");
					}
					$ret = array("esito" => -1, "id" => $request->getParam('id'), "messages" => $messages);
				}
			} else {
				// Servizi ausiliari form
				switch ($request->getParam('function')) {
					case 'add-construction-material':
						$id = $request->getParam('material_order');
						$element = new Zend_Form_Element_Text("new_material_{$id}_name");
						$element->setLabel("Strato " . ((int)$id - 9) . ":")
						->setOptions(array(
							"id" => "new_material_{$id}_name",
							"order" => $id,
							"size" => 60,
							"class" => "idleField validate[required] newmat",
							"style" => "border-color: forestgreen"
						))
						->setRequired(true)
						->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Text("new_material_{$id}_thickness");
						$element->setLabel("Spessore dello strato " . ((int)$id - 9) . ":")
						->setOptions(array(
							"id" => "new_material_{$id}_thickness",
							"order" => $id,
							"size" => 60,
							"class" => "idleField validate[required,custom[number]]",
							"style" => "border-color: forestgreen"
						))
						->setRequired(true)
						->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"))
						->addValidator("Float", false, array("locale" => "en"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Hidden("new_material_{$id}_id");
						$element->setOptions(array(
							"id" => "new_material_{$id}_id",
							"order" => $id,
							"class" => "dyn"
						))
						->clearDecorators()
						->addDecorator('viewHelper');
						$ret .= $element->__toString();
						break;
					default:
						$ret = null;
				}
			}
			echo $ret;
		}
	}
	
	protected function _initForm()
	{
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();

		$browserForm = new Zend_Form(array("id" => "browser"));
		$detailForm = new Zend_Form(array("id" => "detail"));
		$this->_form->addSubForm($browserForm, "browser");
		$this->_form->addSubForm($detailForm, "detail");

		///// TOP (dataTables) /////
		
		Zend_Loader::loadClass('Constructions');
		$costruzioni = new Constructions();
		$recordSet = $costruzioni->select()->from($costruzioni, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->constructions = $costruzioni->fetchAll($recordSet)->toArray();

		///// BOTTOM (zend form) /////

		$campo1 = new Zend_Form_Element_Hidden('material_order');
		$campo1->setValue(10)
		->setOptions(array(
			"id" => "material_order"));
		
		$campo2 = new Zend_Form_Element_Text('construction_name');
		$campo2->setLabel('Nome della costruzione')
		->setOptions(array(
			"id" => "construction_name",
			"order" => 3,
			"size" => 60,
			"class" => "validate[required]"
		))
		->setRequired(true);
		
		
		$campo3 = new Application_Form_Element_ConstructionControls("construction_controls");
		$campo3->setOptions(array("id" => "construction_controls",
			"order" => 2));
		
		$campo4 = new Zend_Form_Element_Text('construction_source');
		$campo4->setLabel('Fonte')
		->setOptions(array(
			"id" => "construction_source",
			"order" => 4,
			"size" => 60));
		
		$campo5 = new Zend_Form_Element_Text('construction_category');
		$campo5->setLabel('Categoria')
		->setOptions(array(
			"id" => "construction_category",
			"order" => 5,
			"size" => 60));
		
		$campo8 = new Zend_Form_Element_Hidden('construction_category_id');
		$campo8->setOptions(array(
			"id" => "construction_category_id",
		));
		
		$campo6 = new Zend_Form_Element_Text('construction_layers');
		$campo6->setLabel('Numero strati')
		->setOptions(array(
			"id" => "construction_layers",
			"order" => 6,
			"size" => 60));
		
		$campo7 = new Zend_Form_Element_Button('material_add');
		$campo7->setLabel('Aggiungi materiale')
		->setOptions(array(
			"id" => "material_add",
			"order" => 7,
			"style" => "margin-bottom: 20px"));
		
		// campi dinamici
		
		$campo100 = new Zend_Form_Element_Text('construction_hci');
		$campo100->setLabel('Coefficiente convettivo di scambio di calore (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_hci",
			"order" => 100,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo101 = new Zend_Form_Element_Text('construction_rad_i');
		$campo101->setLabel('Coefficiente radiativo di scambio di calore (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_rad_i",
			"order" => 101,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo102 = new Zend_Form_Element_Text('construction_sri');
		$campo102->setLabel('Resistenza di superficie (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_sri",
			"order" => 102,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo103 = new Zend_Form_Element_Text('construction_hco');
		$campo103->setLabel('Coefficiente convettivo di scambio di calore (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_hco",
			"order" => 103,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo104 = new Zend_Form_Element_Text('construction_rad_o');
		$campo104->setLabel('Coefficiente radiativo di scambio di calore (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_rad_o",
			"order" => 104,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo105 = new Zend_Form_Element_Text('construction_sro');
		$campo105->setLabel('Resistenza di superficie (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_sro",
			"order" => 105,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo106 = new Zend_Form_Element_Text('construction_uvalue_sts');
		$campo106->setLabel('Valore U superficie su superficie (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_uvalue_sts",
			"order" => 106,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo107 = new Zend_Form_Element_Text('construction_rvalue');
		$campo107->setLabel('Valore R (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_rvalue",
			"order" => 107,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo108 = new Zend_Form_Element_Text('construction_uvalue');
		$campo108->setLabel('Valore U (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_uvalue",
			"order" => 108,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo109 = new Zend_Form_Element_Text('construction_url');
		$campo109->setLabel('Limite di resistenza superiore (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_url",
			"order" => 109,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo110 = new Zend_Form_Element_Text('construction_lrl');
		$campo110->setLabel('Limite di resistenza inferiore (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_lrl",
			"order" => 110,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo111 = new Zend_Form_Element_Text('construction_bs_uvalue_sts');
		$campo111->setLabel('Limite di resistenza inferiore (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_bs_uvalue_sts",
			"order" => 111,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo112 = new Zend_Form_Element_Text('construction_bs_rvalue');
		$campo112->setLabel('Valore R (m^2 K/W)')
		->setOptions(array(
			"id" => "construction_bs_rvalue",
			"order" => 112,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo113 = new Zend_Form_Element_Text('construction_bs_uvalue');
		$campo113->setLabel('Valore U (W/m^2 K)')
		->setOptions(array(
			"id" => "construction_bs_uvalue",
			"order" => 113,
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$this->_form->getSubForm("detail")->addElement($campo1);
		$this->_form->getSubForm("detail")->addElement($campo2);
		$this->_form->getSubForm("detail")->addElement($campo3);
		$this->_form->getSubForm("detail")->addElement($campo4);
		$this->_form->getSubForm("detail")->addElement($campo5);
		$this->_form->getSubForm("detail")->addElement($campo6);
		$this->_form->getSubForm("detail")->addElement($campo7);
		$this->_form->getSubForm("detail")->addElement($campo8);
		$this->_form->getSubForm("detail")->addElement($campo100);
		$this->_form->getSubForm("detail")->addElement($campo101);
		$this->_form->getSubForm("detail")->addElement($campo102);
		$this->_form->getSubForm("detail")->addElement($campo103);
		$this->_form->getSubForm("detail")->addElement($campo104);
		$this->_form->getSubForm("detail")->addElement($campo105);
		$this->_form->getSubForm("detail")->addElement($campo106);
		$this->_form->getSubForm("detail")->addElement($campo107);
		$this->_form->getSubForm("detail")->addElement($campo108);
		$this->_form->getSubForm("detail")->addElement($campo109);
		$this->_form->getSubForm("detail")->addElement($campo110);
		$this->_form->getSubForm("detail")->addElement($campo111);
		$this->_form->getSubForm("detail")->addElement($campo112);
		$this->_form->getSubForm("detail")->addElement($campo113);
		
		// Elementi hidden non devono avere decorators
		$elements = $this->_form->getSubForm("detail")->getElements();
		foreach ($elements as $elem) {
			if ($elem instanceof Zend_Form_Element_Hidden)
				$elem->setDecorators(array('ViewHelper'));
		}
		
		// Datasources
		Zend_Loader::loadClass('ConstructionCategories');
		$categorie = new ConstructionCategories();
		$recordSet = $categorie->select()->from($categorie, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->construction_categories = $categorie->fetchAll($recordSet)->toArray();
		
	}
}
